package com.elephant.core.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Author: hai
 * @Date: 2019/1/11 15:46
 */
public class JavaShellUtils {

    private static final String RUNNING_SHELL_FILE = "run.sh";
    private static final String CASPERJS_FILE_DIR = "/data/zhantuan8066";

    public static void exec() {

        ProcessBuilder pb = new ProcessBuilder("./" + RUNNING_SHELL_FILE, "restart", "8066");
        pb.directory(new File(CASPERJS_FILE_DIR));
        int runningStatus = 0;
        String s;
        try {
            Process p = pb.start();
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }
            try {
                runningStatus = p.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(runningStatus);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        exec();
    }
}
